package solutions;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 黎鹤舞
 * Date: 2023-12-12
 * Time: 12:54
 */
public class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode pre = head;
        ListNode cur = head.next;
        while(cur != null) {
            if(cur.val == val) {
                pre.next = cur.next;
                cur = cur.next;
            }
            cur = cur.next;
            pre = pre.next;
        }
        if(head.val == val) {
            head.next = null;
            head = head.next;
        }
        return head;
    }
}
